<?php
header('Content-Type:text/html;charset=utf-8');
require_once('include/config.php');
require_once('include/wp-db.php');
require_once('include/utils.php');

$time=time();

$d=intval($_REQUEST['d']);

$cookie_username=$_COOKIE['username'];
$cookie_phone=$_COOKIE['phone'];
$cookie_email=$_COOKIE['email'];

if(isset($cookie_username) && (!empty($cookie_username)))
{
		$username=$cookie_username;
	
}
else{
	
	$username='输入你的姓名';
}

if(isset($cookie_phone) && (!empty($cookie_phone)))
{
		$phone=$cookie_phone;
	
}
else{
	
	$phone='输入你的11位手机号码';
}


if(isset($cookie_email) && (!empty($cookie_email)))
{
		$email=$cookie_email;
	
}
else{
	
	$email='输入你的Email地址';
}



$sql_cp="select * from miao_cp where id='$d' and nums>0 and flag=1 ";
$row_cp=$wpdb->get_row($sql_cp);
if($row_cp!=NULL)
{
	$title=$row_cp->title;
	$showsubtitle=$row_cp->subtitle;
	$markprice=$row_cp->markprice;
	$miaoprice_n=$row_cp->miaoprice;
	$showbegintime=$row_cp->begintime;
	$showendtime=$row_cp->endtime;
	$nums      =  $row_cp->nums;
	$str_begintime=$row_cp->str_begintime;
	$str_endtime=$row_cp->str_endtime;
	
	$showimgpath=$row_cp->imgpath;
	$content=$row_cp->content;
	
	$month=date('m',$str_begintime);
	$day=date('d',$str_begintime);
	$hour=date('H',$str_begintime);
	
	$sql_nums="select count(*) from users_cp where cpid='$d'";
	$sub_nums=$wpdb->get_var($sql_nums);
	$sub_nums=intval($nums)-intval($sub_nums);
	
	
	$jgx_sub_nums=$sub_nums;
	
	
}
else
{
	ShowMsg('对不起，没有查到你要的产品','index.php');	
	exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>汉庭海友客栈</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	
	countDown();
	//setTimeout('countDown()',1000);
	
	$('.gtl').click(function(){
		$("#login").before("<div id='exposeMask' style='*height:"+$("body").height()+"px'></div>");
		$("#login").show();
    });

	$('.btn_qr').click(function(){
		
		var username=$('#username').val();
		var phone=$('#phone').val();
		var email=$('#email').val();
		var getid=$('#hid').val();
		var shopname='<?php echo $showsubtitle;?>';
		
		var randNums=rnd(1, 99);
		
		$(".btn_qr").ajaxStart(function(){		
				$('.btn_qr').hide();
				$('.btn_qx').hide();
				$('.waitimg').show();
			});		
			
			
	   $.ajax({
			 type:"POST",
             url:"val_users.php?r="+Math.random(),
			 data:{
				
				username: username,
				phone:phone,
				email:email,
				id:getid
			},
           
             dataType:"text",
             success: function(msg){//如果调用php成功 
		
						
			 if(msg=="-11")
			{
				alert("用户名不正确");
				return false;		
			}
			else if(msg=='-12')
			{
				alert("手机格式不正确");
				return false;	
			}
			
			else if(msg=='-13')
			{
				
				alert("Email格式不正确");
				return false;	
			}
			
			else if(msg=='-14')   //产品不存在
			{
				alert("Email格式不正确");
				return false;	
			}
			
			else if(msg=='-15')   //秒杀过次产品
			{
				
			//	$('.txt').html(username+",您未能秒杀成功。<br /><strong>不要气馁，再看看其他秒杀吧！</strong>");
				$('.showname').html(username);
				$("#error").show();
				
			}
			
			else if(msg=='-16')  //没有秒杀成功
			{
				$('.showname').html(username);
				$("#fail").show();
			}
			
			
			else if(msg=='-17')  //没有秒杀成功
			{
				$('.showname').html(username);
				$("#fail").show();
			}
			
			else if(msg=="999")
			{
					
				$('.showname').html(username);
				$('#success_txt').html("<strong>"+shopname+"&nbsp;&nbsp;&nbsp;数量：1</strong><br />您的房券编码将发送到手机："+phone+"，及邮箱"+email+"，<br />请注意查收,一周内预订有效，谢谢！<br/><a href='http://www.htinns.com/hanting/KZDefault.aspx' target='_blank' style='color:#069;text-decoration:underline'>立即前往汉庭官网预定</a>");
				$("#success").show();
				//window.open("http://www.htinns.com/hanting/KZDefault.aspx");
			}
		 }
	   });  //end ajax
	   
	   
	   $(".btn_qr").ajaxStop(function(){		
				$('.btn_qr').show();
				$('.btn_qx').show();
				$('.waitimg').hide();
			});	
		
    });
	$('.close').click(function(){
		$("#exposeMask").remove();
		$("#login").hide();
		$("#info").hide();
		$("#success").hide();
		$("#fail").hide();
		$("#error").hide();
    });
	$('.btn_qx').click(function(){
		$("#exposeMask").remove();
		$("#info").hide();
    });
	
	//btn_bcxx
	$('.btn_bcxx').click(function(){
		
		var username=$('#username').val();
		var phone=$('#phone').val();
		var email=$('#email').val();
		
		if( username == "" || username == null || username=='输入你的姓名' ){
		  alert('姓名不能为空'); 
		  $('#username').focus();
		  return false;
		}
		else
		{
			if(username.length>8 || username.length<2) {
				
			  alert('姓名的长度只能为2-8个字符'); 
			    $('#username').focus();
			  return false;	
			}else{
				
			  if(!/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/.test(username)){
				 alert('姓名只能是字母，数字，下划线和中文'); 
				   $('#username').focus();
			     return false;	
				}
			  	
			}
		}
		
		if(phone=='' || phone==null || phone=='输入你的11位手机号码')
		{
			
			 alert('手机号码不能为空'); 
			 $('#phone').focus();
			 return false;	
		}
		else 
		{
			if(!isPhone(phone))
			{
				alert("您输入的手机号码格式不正确，请输入正确的11位手机号码！")
				 $('#phone').focus();
				return false;
			}
			
		}
		
		if(email=='' || email==null || email=='输入你的Email地址')
		{
			
			 alert('您的Email号码不能为空'); 
			 $('#email').focus();
			 return false;	
		}
		else 
		{
			if(!isValidMail(email))
			{
				alert("您输入的Email格式不正确，请输入正确的Email地址！")
				 $('#email').focus();
				return false;
			}
			
		}
		
			$(".btn_bcxx").ajaxStart(function(){		
				$('.btn_bcxx').hide();
				$('#mm_y').hide();
				$('.waitimg').show();
			});		
		 $.ajax({
			 type:"POST",
             url:"save_users.php?r="+Math.random(),
			 data:{
				
				username: username,
				phone:phone,
				email:email
			},
           
             dataType:"text",
             success: function(msg){//如果调用php成功 
		
						
			 if(msg=="-11")
			{
				alert("用户名不正确");
				return false;		
			}
			else if(msg=='-12')
			{
				alert("手机格式不正确");
				return false;	
			}
			
			else if(msg=='-13')
			{
				
				alert("Email格式不正确");
				return false;	
			}
			
			
			else if(msg=="999")
			{
					
				window.location.reload(true);
			}
		 }
	   });  //end ajax
		
		 $(".btn_bcxx").ajaxStop(function(){		
				$('.btn_bcxx').show();
				$('#mm_y').show();
				$('.waitimg').hide();
			});	
		
		
		
		//$('#showusername').html(username);
//		$('#showname').html(username);
//		$('#showphone').html(phone);
//		$('#showemail').html(email);
//		
//		$("#login").before("<div id='exposeMask' style='*height:"+$("body").height()+"px'></div>");
//		$("#info").show();
		
	
});
	
	
	$('#btn_buy').click(function(){
		
		var username=$('#username').val();
		var phone=$('#phone').val();
		var email=$('#email').val();
		
		if( username == "" || username == null || username=='输入你的姓名' ){
		  alert('姓名不能为空'); 
		  $('#username').focus();
		  return false;
		}
		else
		{
			if(username.length>8 || username.length<2) {
				
			  alert('姓名的长度只能为2-8个字符'); 
			    $('#username').focus();
			  return false;	
			}else{
				
			  if(!/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/.test(username)){
				 alert('姓名只能是字母，数字，下划线和中文'); 
				   $('#username').focus();
			     return false;	
				}
			  	
			}
		}
		
		if(phone=='' || phone==null || phone=='输入你的11位手机号码')
		{
			
			 alert('手机号码不能为空'); 
			 $('#phone').focus();
			 return false;	
		}
		else 
		{
			if(!isPhone(phone))
			{
				alert("您输入的手机号码格式不正确，请输入正确的11位手机号码！")
				 $('#phone').focus();
				return false;
			}
			
		}
		
		if(email=='' || email==null || email=='输入你的Email地址')
		{
			
			 alert('您的Email号码不能为空'); 
			 $('#email').focus();
			 return false;	
		}
		else 
		{
			if(!isValidMail(email))
			{
				alert("您输入的Email格式不正确，请输入正确的Email地址！")
				 $('#email').focus();
				return false;
			}
			
		}
		
		$('#showusername').html(username);
		$('#showname').html(username);
		$('#showphone').html(phone);
		$('#showemail').html(email);
		
		$("#login").before("<div id='exposeMask' style='*height:"+$("body").height()+"px'></div>");
		$("#info").show();
		
	})
	
});

 var NowTime = new Date();

				  //计算出服务器和客户端的时间差。
 var dTime = <?php echo   $time*1000 ;?>-NowTime.getTime();
function countDown(){
	var all = $("input[id^='item_ids_']");
	for (i=0; i< all.length; i++)
	{
		var id = all[i].value;
		
	   var NowTime = new Date();
	   var sutime= NowTime.getTime();
	   var lasttime = $("#lasttime_" + id).val();
	   var starttime= $("#stime_" + id).val();
	   var showtime=$("#showtime_" + id).val();
		
	    lasttime=lasttime*1000;
		
		var s_l_time=0;
		s_l_time=<?php echo  $time ;?>-starttime;
		
	    var nowtime = Date.parse(new Date());
		var time = lasttime- nowtime-dTime;
		var str='';
		
		var sub_nums='<?php echo $sub_nums?>';
		if(sub_nums<=0)
		{
			$("#item_time_display_" + id).html('秒杀已结束');
			$('#miao_yes').hide();	
			$('#miao_no').hide();
			$('#show_sub_nums').hide();
		}
		else
		{
	
			if(s_l_time<=0)
			{
				//str=showtime;
					str="<span>开始日期&nbsp;&nbsp;</span><strong><?php echo $month?></strong><span>月</span><strong><?php echo $day?></strong><span>日</span><strong><?php echo $hour?></strong><span>时</span>";		
				$('#miao_yes').hide();	
				$('#miao_no').show();	
				$('#show_sub_nums').show();
	
				$("#item_time_display_" + id).empty();
				$("#item_time_display_" + id).html(str);	
				
			}
			else if (Math.floor(time) <= 0)
			{
				$("#item_time_display_" + id).html('秒杀已结束');
				$('#miao_yes').hide();	
				$('#miao_no').show();
				$('#show_sub_nums').hide();
			}
			else
			{
				var tday=Math.floor(time/(86400*1000));
				var thour=Math.floor(time/(3600*1000))%24;
				
				var totalhour=tday*24+thour;
				var tminute=Math.floor(time/(60*1000))%60;
				var tsecond=Math.floor(time/1000)%60;
				str="<span>距离结束&nbsp;&nbsp;</span><strong>"+totalhour+"</strong><span>小时</span><strong>"+tminute+"</strong><span>分</span><strong>"+tsecond+"</strong><span>秒</span>";	
				$('#miao_yes').show();	
				$('#miao_no').hide();		
				$('#show_sub_nums').hide();
				$("#item_time_display_" + id).empty();
				$("#item_time_display_" + id).html(str);
			}
		}
	}
	setTimeout('countDown()',1000);
}

function rnd(start, end){
    return Math.floor(Math.random() * (end - start) + start);
}
</script>
</head>
<body>
<div id="header">
	<div class="logo"><a href="index.php"></a></div>
	<div class="menu">
		<ul>
			<li><a href="./"><img src="images/menu_1.png" alt="首页" /></a></li>
			<li><a href="list.php"><img src="images/menu_2_.png" alt="今日秒杀" /></a></li>
			<li><a href="next.php"><img src="images/menu_3.png" alt="下期预告" /></a></li>
			<li><a href="photo.php"><img src="images/menu_4.png" alt="客栈相册" /></a></li>
			<li><a href="qa.php"><img src="images/menu_5.png" alt="常见问题" /></a></li>
		</ul>
	</div>
	<div class="banner">
		<div class="nav">
			<ul>
				<li><a class="gtl"><img src="images/nav_login.png" /></a></li>
				<li><a href="./h/index.html"><img src="images/nav_hello.png" /></a></li>
				<li><a href="explain.php"><img src="images/nav_explain.png" /></a></li>
				<li><a href="http://www.htinns.com/hanting/KZDefault.aspx"  target="_blank"><img src="images/nav_website.png" /></a></li>
			</ul>
		</div>
		<div class="share">
			<a onclick="jiathis_sendto('tsina');return false;" style="cursor:pointer"><img src="images/ico_tsina.png" title="新浪微博" /></a>
			<a onclick="jiathis_sendto('tqq');return false;"  style="cursor:pointer"><img src="images/ico_tqq.png" title="腾讯微博" /></a>
			<a onclick="jiathis_sendto('qzone');return false;"  style="cursor:pointer"><img src="images/ico_qzone.png" title="QQ空间" /></a>
			<a onclick="jiathis_sendto('renren');return false;"  style="cursor:pointer"><img src="images/ico_renren.png" title="人人网" /></a> 
			<a onclick="jiathis_sendto('douban');return false;"  style="cursor:pointer"><img src="images/ico_douban.png" title="豆瓣" /></a>
			<a onclick="jiathis_sendto('kaixin001');return false;"  style="cursor:pointer"><img src="images/ico_kaixin001.png" title="开心网" /></a>
			<script type="text/javascript">
				var jiathis_config = {
				url: "http://miaosha.htinns.com/",
				title: "我以给力价成功秒杀到海友客栈房间啦，大家赶紧来抢啊！"
				
				}
			</script>
			
			
			<script type="text/javascript" src="js/share.js"></script> 
		</div>
	</div>
</div>
<div id="main" class="fix">
	<?php include('left.php');?>
	<div class="right">
		<div style="padding:30px 0 0 40px">
        	 <h2><?php echo $title;?></h2>
			<div class="deal-intro"> 	
            
           
				<div class="pics"><img src="upload/hotel/<?php echo $showimgpath;?>" /><b id="show_sub_nums">秒杀数量：<?php echo $jgx_sub_nums?></b></div>
				<div class="side">
                <input type="hidden" name="hid" value="<?php echo $d?>" id="hid" />
					<table cellpadding="0" cellspacing="0">
						<tr>
							<th>市场价：</th>
							<td><em>￥<?php echo $markprice;?></em></td>
							<th>秒杀价：</th>
							<td><b>￥<?php  echo $miaoprice_n;?></b></td>
						</tr>
					</table>
					<p  id="item_time_display_<?php echo $d?>"></p>
					<input id="lasttime_<?php echo $d?>" type="hidden" value="<?php echo $str_endtime;?>"/>
					<input id="item_ids_<?php echo $d?>" type="hidden" value="<?php echo $d?>"/>
               		<input id="stime_<?php echo $d?>" type="hidden" value="<?php echo $str_begintime?>"/>
                    
                    
                    
					<ul <?php if($time>=$str_endtime || $jgx_sub_nums<=0){ printf("style='display:none'");}?>>
						<li><span>姓名：</span><label><input type="text" onFocus="if(this.value=='输入你的姓名')this.value='';" onBlur="if(this.value=='')this.value='输入你的姓名';" value="<?php echo $username;?>" id="username" name="username" /></label></li>
						<li><span>手机：</span><label><input type="text" onFocus="if(this.value=='输入你的11位手机号码')this.value='';" onBlur="if(this.value=='')this.value='输入你的11位手机号码';" value="<?php echo $phone;?>"  id="phone"  name="phone"/></label></li>
						<li><span>邮箱：</span><label><input type="text" onFocus="if(this.value=='输入你的Email地址')this.value='';" onBlur="if(this.value=='')this.value='输入你的Email地址';" value="<?php echo $email?>"  id="email" name="email"/></label></li>
						<li id="miao_yes" style="display:none"><span></span><label><a class="btn_buy" id="btn_buy" style="cursor:pointer"><img src="images/btn_buy.png" alt="即刻秒杀" /></a></label></li>
						<li id="miao_no"><i class="waitimg" style="display:none;"><img  src="images/20-0.gif"  align="middle"/>&nbsp;&nbsp;&nbsp;正在提交,请等待...</i><input class="btn_bcxx"   type="image" src="images/btn_bcxx.png" /><b id="mm_y">* 立即填写个人信息，准备抢秒</b>
                        
                        
                        </li>
					</ul>
				</div>
				<div class="txt">
					<strong><?php echo $showsubtitle;?></strong><br />
					<?php echo $content;?>
				</div>
			</div>
		</div>
	</div>
</div>
<?php include('footer.php');?>
<?php include('logintemplate.php');?>
<div class="simple_overlay" id="info">
	<div class="bg"></div>
	<div style="position:relative">
		<form>
			<div style="padding:10px 0 0 20px;height:30px"></div>
			<div style="padding:0 0 0 50px">
				<table cellpadding="0" cellspacing="0">
					<tr>
						<th>姓名</th>
						<td id="showusername"></td>
					</tr>
					<tr>
						<th>手机</th>
						<td id="showphone"></td>
					</tr>
					<tr>
						<th>邮箱</th>
						<td id="showemail"></td>
					</tr>
				</table>
			</div>
			<div style="padding:15px 0 0 50px">
				<input type="button"  class='btn_qr' title='确认秒杀' value='' style="cursor:pointer" />&nbsp;
				<input type="button"   class='btn_qx' title='取消' value='' style="cursor:pointer"/>
            <span class="waitimg"  style="display:none;" ><img  src="images/20-0.gif"   align="middle"/>&nbsp;&nbsp;&nbsp;正在提交,请等待...</span>
			</div>
		</form>
	</div>
	<img src='images/close.png' class='close' />
</div>
<div class="simple_overlay" id="success">
	<div class="bg"></div>
	<div style="position:relative">
		<h3>恭喜<strong><span class="showname" ></span></strong>, 秒杀成功！</h3>
		<div class="txt" id="success_txt">
		</div>
		<div class="share"><img src="images/ico_share.gif" />
			<a onclick="jiathis_sendto('tsina');return false;"><img src="images/ico_tsina.gif" title="新浪微博" /></a>
			<a onclick="jiathis_sendto('tqq');return false;"><img src="images/ico_tqq.gif" title="腾讯微博" /></a>
			<a onclick="jiathis_sendto('qzone');return false;"><img src="images/ico_qzone.gif" title="QQ空间" /></a>
			<a onclick="jiathis_sendto('renren');return false;"><img src="images/ico_renren.gif" title="人人网" /></a> 
			<a onclick="jiathis_sendto('douban');return false;"><img src="images/ico_douban.gif" title="豆瓣" /></a>
			<a onclick="jiathis_sendto('kaixin001');return false;"><img src="images/ico_kaixin001.gif" title="开心网" /></a>
		</div>
	</div>
	<img src='images/close.png' class='close' />
</div>
<div class="simple_overlay" id="fail">
	<div class="bg"></div>
	<div style="position:relative">
		<h3>很遗憾！<strong><span class="showname" ></span></strong></h3>
		<div class="txt" id="fail_txt">
			您未能秒杀成功。<br />
			<strong>不要气馁，再看看其他秒杀吧！</strong>
		</div>
	</div>
	<img src='images/close.png' class='close' />
</div>
<div class="simple_overlay" id="error">
	<div class="bg"></div>
	<div style="position:relative">
		<h3>很遗憾！<strong><span class="showname" ></span></strong></h3>
		<div class="txt" id="error_txt">
			 您已成功秒杀过该产品，不可重复操作。<br />
			<strong>同一款产品，同一手机号只限秒杀一次</strong>
		</div>
	</div>
	<img src='images/close.png' class='close' />
</div>
</body>
</html>
